package cn.gong.movie.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import cn.gong.movie.bean.Drama;
import cn.gong.movie.bean.Movie;
import framework.base.BaseDb;
import framework.database.annotaion.DBField;

public class DbDrama extends BaseDb {
	public static final String TABLE = "drama";

	@DBField(ID_PK_AUTO)
	public static final String ID = "id";
	@DBField(VARCHAR)
	public static final String NAME = "name";
	@DBField(VARCHAR + "(8)")
	public static final String DATE = "date";
	@DBField(INTEGER)
	public static final String TOTAL_COUNT = "total_count";
	@DBField(INTEGER)
	public static final String WATCH_TIME = "watch_time";
	@DBField(VARCHAR)
	public static final String SEEN = "seen";
	@DBField(INTEGER)
	public static final String STATUS = "status";
	@DBField(INTEGER)
	public static final String SEEN_COUNT = "seen_count";

	// public Drama setDrama(Context context, Drama drama) {
	// SQLiteDatabase db = getDb(context);
	//
	// ContentValues values = new ContentValues();
	// values.put(NAME, drama.getName());
	// values.put(DATE, drama.getDate());
	// values.put(TOTAL_COUNT, drama.getTotalNum());
	// values.put(SEEN, "1,5");
	// values.put(WATCH_TIME, 1375088478480l);
	// values.put(STATUS, 0);
	//
	// for (int i = 0; i < 20; i++) {
	// long id = db.insert(TABLE, null, values);
	// }
	//
	// closeDb(db);
	// db = null;
	//
	// return drama;
	// }

	public Drama setDrama(Context context, Drama drama) {
		SQLiteDatabase db = getDb(context);

		ContentValues values = new ContentValues();
		values.put(NAME, drama.getName());
		values.put(DATE, drama.getDate());
		values.put(TOTAL_COUNT, drama.getTotalNum());
		values.put(STATUS, drama.getStatus());
		values.put(SEEN_COUNT, drama.getSeenNum());
		values.put(WATCH_TIME, drama.getWatchTime());
		long id = db.insert(TABLE, null, values);

		drama.setId(id);

		closeDb(db);
		db = null;

		return drama;
	}

	// select * from drama where status=0 ORDER BY watch_time desc limit 3,3 ;
	public ArrayList<Drama> getFollowDramas(Context context, long followWatchTime) {

		int perPage = 20;

		SQLiteDatabase db = getDb(context);
		String selection = null;
		String[] selectionArgs = null;

		log("followWatchTime:" + followWatchTime);
		if (followWatchTime > 0) {
			selection = STATUS + "=? and " + WATCH_TIME + "<?";
			selectionArgs = new String[] { String.valueOf(0), String.valueOf(followWatchTime) };
		} else {
			selection = STATUS + "=?";
			selectionArgs = new String[] { String.valueOf(0) };
		}

		Cursor cursor = db.query(TABLE, null, selection, selectionArgs, null, null, WATCH_TIME + " desc", 0 + "," + perPage);
		ArrayList<Drama> dramas = new ArrayList<Drama>();
		while (cursor.moveToNext()) {
			Drama drama = new Drama();
			drama.setId(getCursorLong(cursor, ID));
			drama.setName(getCursorString(cursor, NAME));
			drama.setDate(getCursorString(cursor, DATE));
			drama.setStatus(getCursorInt(cursor, STATUS));
			drama.setTotalNum(getCursorInt(cursor, TOTAL_COUNT));
			drama.setWatchTime(getCursorLong(cursor, WATCH_TIME));

			String seenStr = getCursorString(cursor, SEEN);
			boolean[] episodes = new boolean[drama.getTotalNum()];
			if (!TextUtils.isEmpty(seenStr)) {
				String[] split = seenStr.split(",");
				if (split != null && split.length > 0) {
					for (int i = 0; i < split.length; i++) {
						int episode = Integer.parseInt(split[i]);
						episodes[episode - 1] = true;
					}
					drama.setSeenNum(split.length);
				}
			}

			drama.setEpisodes(episodes);
			dramas.add(drama);
		}
		closeDbAndCursor(db, cursor);
		db = null;
		cursor = null;
		return dramas;
	}

	// select * from drama where status=1 limit 3,3 ;
	public ArrayList<Drama> getWantDramas(Context context, long lastIndex) {
		int perPage = 20;

		SQLiteDatabase db = getDb(context);
		String selection = null;
		String[] selectionArgs = null;
		if (lastIndex > 0) {
			selection = STATUS + "=? and " + ID + "<?";
			selectionArgs = new String[] { String.valueOf(1), String.valueOf(lastIndex) };
		} else {
			selection = STATUS + "=?";
			selectionArgs = new String[] { String.valueOf(1) };
		}

		Cursor cursor = db.query(TABLE, null, selection, selectionArgs, null, null, ID + " desc", 0 + "," + perPage);
		ArrayList<Drama> dramas = new ArrayList<Drama>();
		while (cursor.moveToNext()) {
			Drama drama = new Drama();
			drama.setId(getCursorLong(cursor, ID));
			drama.setName(getCursorString(cursor, NAME));
			drama.setDate(getCursorString(cursor, DATE));
			drama.setStatus(getCursorInt(cursor, STATUS));
			drama.setTotalNum(getCursorInt(cursor, TOTAL_COUNT));
			drama.setEpisodes(new boolean[drama.getTotalNum()]);
			drama.setSeenNum(0);
			dramas.add(drama);
		}
		closeDbAndCursor(db, cursor);
		db = null;
		cursor = null;
		return dramas;
	}

	// select * from drama where status=2 limit 3,3 ;
	public ArrayList<Drama> getSeenDramas(Context context, long lastIndex) {
		int perPage = 20;

		SQLiteDatabase db = getDb(context);
		
		String selection = null;
		String[] selectionArgs = null;
		if (lastIndex > 0) {
			selection = STATUS + "=? and " + ID + "<?";
			selectionArgs = new String[] { String.valueOf(1), String.valueOf(lastIndex) };
		} else {
			selection = STATUS + "=?";
			selectionArgs = new String[] { String.valueOf(1) };
		}
		
//		String selection = STATUS + "=?";
//		String[] selectionArgs = new String[] { String.valueOf(2) };

		Cursor cursor = db.query(TABLE, null, selection, selectionArgs, null, null, ID + " desc", 0 + "," + perPage);
		ArrayList<Drama> dramas = new ArrayList<Drama>();
		while (cursor.moveToNext()) {
			Drama drama = new Drama();
			drama.setId(getCursorLong(cursor, ID));
			drama.setName(getCursorString(cursor, NAME));
			drama.setDate(getCursorString(cursor, DATE));
			drama.setStatus(getCursorInt(cursor, STATUS));
			drama.setTotalNum(getCursorInt(cursor, TOTAL_COUNT));
			drama.setSeenNum(getCursorInt(cursor, SEEN_COUNT));

			boolean[] episodes = new boolean[drama.getTotalNum()];
			for (int i = 0; i < drama.getTotalNum(); i++) {
				episodes[i] = true;
			}
			drama.setEpisodes(episodes);
			dramas.add(drama);
		}
		closeDbAndCursor(db, cursor);
		db = null;
		cursor = null;
		return dramas;
	}

	public Drama updateDrama(Context context, Drama drama) {
		SQLiteDatabase db = getDb(context);

		ContentValues values = new ContentValues();
		values.put(NAME, drama.getName());
		values.put(TOTAL_COUNT, drama.getTotalNum());
		values.put(SEEN_COUNT, drama.getSeenNum());
		values.put(DATE, drama.getDate());
		values.put(SEEN, drama.getEpisodeStr());
		values.put(WATCH_TIME, drama.getWatchTime() + "");
		values.put(STATUS, drama.getStatus());

		int update = db.update(TABLE, values, ID + "=?", new String[] { drama.getId() + "" });
		closeDb(db);
		db = null;

		return drama;
	}

	public Object deleteDrama(Context context, Drama drama) {
		SQLiteDatabase db = getDb(context);

		db.delete(TABLE, ID + "=?", new String[] { drama.getId() + "" });

		closeDb(db);
		db = null;

		return null;
	}

}
